<?php
apf_require_controller("HrAbstract");

class Hr_Job_ListController extends HrAbstractController {

    const PAGE_ROWS = 10;
    const PAGE_NUM_SIZE=10;

    public function handle_request() {
        $params = $this->request->get_parameters();
        $page = intval($params['p']);
        $page = $page<1?1:$page;
        $offset = ($page-1)*self::PAGE_ROWS;

        $where = $this->_build_where($params);
        $bll = new Bll_Hr_Property();
        $selectdata['company'] = $bll->get_company_list();
        $selectdata['citylist'] = $bll->get_sort_city_list();
        $selectdata['business'] = $bll->get_business_list();
        $selectdata['function'] = $bll->get_function_list();

        $job_bll = new Bll_Hr_Job();
        $dep_bll = new Bll_Hr_Department();
        //共有个部门
        $data['dep_count'] = $dep_bll->get_departmeng_count($where);
        $data['job_count'] = $job_bll->get_job_count($where);
        //分页
        $dep_list = $dep_bll->get_departmeng_list($where,self::PAGE_ROWS,$offset);
        $data['dep_list'] = $dep_bll->build_data($dep_list,$selectdata);

        $uri_params = array('p' => $page);
        $uri_params = array_merge($uri_params,$where);//合并查询条件
        unset($uri_params['is_deleted']);
        $url_suffix = "";
        $url_prefix = Util_BaseUrls::job_list_url();
        $total = $data['dep_count'];
        if($total > self::PAGE_ROWS){
            $data['multipage'] = Util_MultiPage::multiPage($url_prefix, $uri_params, $url_suffix, $total, self::PAGE_ROWS, self::PAGE_NUM_SIZE, false, true);
        }

        $this->request->set_attribute('selectdata', $selectdata);
        $this->request->set_attribute('params', $uri_params);
        $this->request->set_attribute('data', $data);
        return "Hr_Job_List";
    }

    /**
     * 重组数据
     * @param array $list
     * @param array $pro_list
     */
    private function _build_data($data,$pro_list){
        $result=array();
        foreach($data as $k=>$val){
            $val['city_name'] = $pro_list['citylist'][$val['city_id']];
            $val['bu_name'] = $pro_list['business'][$val['bu_id']]['bu_name'];
            $val['function_name'] = $pro_list['function'][$val['function_id']]['function_name'];
            $result[$k] = $val;
        }
        return $result;
    }

    private function _build_where($params){
        $_where = array();
        if(!empty($params['company_id'])){
            $_where['company_id']=intval($params['company_id']);
        }
        if(!empty($params['city_id'])){
            $_where['city_id']=intval($params['city_id']);
        }
        if(!empty($params['bu_id'])){
            $_where['bu_id']=intval($params['bu_id']);
        }
        if(!empty($params['function_id'])){
            $_where['function_id']=intval($params['function_id']);
        }
        $_where['is_deleted']=1;
        return $_where;
    }
}